﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Book
{
    public partial class BookInfo : Form
    {
        public BookInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BookInfo_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var res = DbHelper.GetSelect(sql);
            dataGridView1.DataSource = res;

            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;

            dataGridView1.MultiSelect = false;  
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Select_Click(object sender, EventArgs e)
        {
            string title = txt_Title.Text;
            string author = txt_Author.Text;
            if (title == "" && author == "")
            {
                string sql = "select * from Articles";
                var res = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = res;
            }
            else if (title != "" && author == "")
            {
                string sql = string.Format("select * from Articles where Title like '%{0}%'", title);
                var res = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = res;
            }
            else if (author != "" &&  title == "")
            {
                string sql = string.Format("select * from Articles where Author like '%{0}%'", author);
                var res = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = res;
            }
            else
            {
                MessageBox.Show("查询出错！");
            }
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddInfo add = new AddInfo();
            add.Show();
            this.Hide();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count >0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var time = (DateTime)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                //实例化修改信息窗体并进行传值
                UpdateInfo update = new UpdateInfo( id,title, author, time);
                update.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("你没有选中任何的行！", "警告");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Delect_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count >0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var isOk = MessageBox.Show("是否确认删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (isOk.Equals(DialogResult.Yes))
                {
                    string sql = string.Format("delete from Articles where Id={0}", id);
                    var res = DbHelper.GetAddOrDelOrUp(sql);
                    if (res > 0)
                    {
                        var title = txt_Title.Text;
                        MessageBox.Show("删除成功！", "消息提示");
                        var sql1 = string.Format("select * from Articles where Title='{0}'",title);
                        var dt = DbHelper.GetSelect(sql1);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
            else
            {
                MessageBox.Show("你没有选中任何的行！", "消息提示");
            }
        }
    }
    }
    

